package com.tianyang.yukuaifuzs.business.main.model;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.tianyang.yukuaifuzs.business.login.bean.LoginOutput;
import com.tianyang.yukuaifuzs.business.main.bean.AppVersionOutput;
import com.tianyang.yukuaifuzs.business.main.bean.ChannelOutput;
import com.tianyang.yukuaifuzs.business.main.bean.DeviceOutput;
import com.tianyang.yukuaifuzs.business.main.bean.HelpCenterOutput;
import com.tianyang.yukuaifuzs.business.main.bean.PosAgentOutput;
import com.tianyang.yukuaifuzs.business.main.bean.PosRateOutput;
import com.tianyang.yukuaifuzs.business.main.bean.PosUserOutput;
import com.tianyang.yukuaifuzs.business.main.bean.UserCenterOutput;
import com.tianyang.yukuaifuzs.business.main.bean.UserStoreOutput;
import com.tianyang.yukuaifuzs.common.cache.CacheManage;
import com.tianyang.yukuaifuzs.common.cache.CacheModel;
import com.tianyang.yukuaifuzs.common.interfaces.CommonConfig;
import com.tianyang.yukuaifuzs.rxservice.client.PlateAgentClient;
import com.tianyang.yukuaifuzs.rxservice.client.PlateUserClient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observable;

/**
 * Description：Main模块model
 */

public class MainModel {

    private static volatile MainModel mInstance;

    private MainModel() {
        if (mInstance != null) {
            throw new IllegalStateException("Already initialized.");
        }
    }

    public static MainModel getInstance() {
        MainModel result = mInstance;
        if (result == null) {
            synchronized (MainModel.class) {
                result = mInstance;
                if (result == null) {
                    mInstance = result = new MainModel();
                }
            }
        }
        return result;
    }

    /**
     * 获取用户信息
     */
    public Observable<UserCenterOutput> getUserInfo() {
        return PlateAgentClient.getInstance().getUserInfo();
    }

    public Observable<List<PosRateOutput>> getPosTemplateRate(String userName, int type, String posChannel) {
        Map<String, Object> reqData = new HashMap<>();
        reqData.put("phone", userName);
        reqData.put("type", type);
        reqData.put("posChannel", posChannel);
        return PlateAgentClient.getInstance().getPosTemplateRate(reqData);
    }

    public Observable<JSONObject> addPosUserRate(List<PosRateOutput> outputs, String userName, int type, String channel) {
        Map<String, Object> reqData = new HashMap<>();
        reqData.put("list", outputs);
        reqData.put("phone", userName);
        reqData.put("type", type);
        reqData.put("freezeAmount", 0);
        reqData.put("channel", channel);
        return PlateAgentClient.getInstance().addPosUserRate(reqData);
    }

    public Observable<List<PosUserOutput>> posUserManage(int pageNum, String phone, int posStatus, int merchantType) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("pageSize", CommonConfig.PAGE_SIZE+"");
        reqData.put("pageNum", pageNum + "");
        if (!TextUtils.isEmpty(phone))
            reqData.put("phone", phone);
        if (posStatus >= 0) {
            reqData.put("posStatus", posStatus + "");
        }
        if (merchantType >= 0) {
            reqData.put("merchantType", merchantType + "");
        }
        return PlateAgentClient.getInstance().posUserManage(reqData);
    }

    public Observable<List<PosAgentOutput>> posAgentManage(int pageNum, String phone) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("pageSize", CommonConfig.PAGE_SIZE + "");
        reqData.put("pageNum", pageNum + "");
        if (!TextUtils.isEmpty(phone))
            reqData.put("phone", phone);
        return PlateAgentClient.getInstance().posAgentManage(reqData);
    }

    public Observable<List<UserStoreOutput>> userMerchantStoresList() {
        Map<String, String> reqData = new HashMap<>();
        return PlateUserClient.getInstance().userMerchantStoresList(reqData);
    }

    public Observable<LoginOutput> getAnotherStoresToken(int memberId) {
        return PlateUserClient.getInstance().getAnotherStoresToken(memberId);
    }

    public Observable<JSONObject> createNewStoreMerchant(String merchantType, String posChannel) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("merchantType", merchantType);
        reqData.put("channel", posChannel);
        return PlateAgentClient.getInstance().createNewStoreMerchant(reqData);
    }

    public Observable<JSONObject> compositePoster(String inviteCode, int type) {
        Map<String, Object> reqData = new HashMap<>();
        reqData.put("type", type);
        reqData.put("inviteCode", inviteCode);
        return PlateAgentClient.getInstance().compositePoster(reqData);
    }

    public Observable<JSONObject> confirmPosBind(String sn) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("sn", sn);
        return PlateUserClient.getInstance().confirmPosBind(reqData);
    }

    public Observable<JSONObject> getCompositePoster(String mchName, String qrCodeUrl, String userId) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("waterMarkContent", mchName);
        reqData.put("qrCodeUrl", qrCodeUrl);
        if (!TextUtils.isEmpty(userId)) {
            reqData.put("userId", userId);
        }
        return PlateUserClient.getInstance().getCompositePoster(reqData);
    }

    public Observable<JSONObject> cancellationAccount() {
        return PlateUserClient.getInstance().cancellationAccount();
    }

    public Observable<HelpCenterOutput> getServicePhone() {
        return PlateAgentClient.getInstance().getServicePhone();
    }

    public Observable<AppVersionOutput> getAppVersionInfo() {
        return PlateAgentClient.getInstance().getAppVersionInfo();
    }

    public Observable<JSONObject> getDeviceTypeList(String userId) {
        Map<String, String> reqData = new HashMap<>();
        if (!TextUtils.isEmpty(userId)) {
            reqData.put("userId", userId);
        }
        return PlateUserClient.getInstance().getDeviceTypeList(reqData);
    }

    public Observable<List<DeviceOutput>> getDeviceList(String type, String userId) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("type", type);
        if (!TextUtils.isEmpty(userId)) {
            reqData.put("userId", userId);
        }
        return PlateUserClient.getInstance().getDeviceList(reqData);
    }

    public Observable<JSONObject> bindingDevice(String type, String sn, String userId) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("type", type);
        reqData.put("sn", sn);
        if (!TextUtils.isEmpty(userId)) {
            reqData.put("userId", userId);
        }
        return PlateUserClient.getInstance().bindingDevice(reqData);
    }

    public Observable<JSONObject> modifyDeviceName(String id, String posName, String userId) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("id", id);
        reqData.put("posName", posName);
        if (!TextUtils.isEmpty(userId)) {
            reqData.put("userId", userId);
        }
        return PlateUserClient.getInstance().modifyDeviceName(reqData);
    }

    public Observable<JSONObject> relationDevice(String id, String bindPosNumber, String userId) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("id", id);
        reqData.put("bindPosNumber", bindPosNumber);
        if (!TextUtils.isEmpty(userId)) {
            reqData.put("userId", userId);
        }
        return PlateUserClient.getInstance().relationDevice(reqData);
    }

    public Observable<JSONObject> unRelationDevice(String id, String userId) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("id", id);
        if (!TextUtils.isEmpty(userId)) {
            reqData.put("userId", userId);
        }
        return PlateUserClient.getInstance().unRelationDevice(reqData);
    }

    public Observable<List<ChannelOutput>> getChannelList() {
        return PlateAgentClient.getInstance().getChannelList();
    }

    public Observable<JSONObject> agentRegisterNew(String agentName, String phone, String password) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("agentName", agentName);
        reqData.put("phone", phone);
        reqData.put("password", password);
        UserCenterOutput centerOutput = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (centerOutput != null) {
            reqData.put("inviteAgentPhoneNo", centerOutput.getAgentPhone());
        }
        return PlateAgentClient.getInstance().agentRegisterNew(reqData);
    }
}
